import pygame,sys
from pygame.locals import *

class BackpackBase(pygame.sprite.Sprite):
    def __init__(self,width,height):
        pygame.sprite.Sprite.__init__(self)

        self.width,self.height=width,height
        self.image=pygame.image.load('images/backpack_base.png').convert_alpha()
        self.rect=self.image.get_rect()
        self.rect.left,self.rect.top=0,0
        self.switch=False

    def arise(self):
        self.rect.left,self.rect.top=0,0

    def move(self,mouse_pos):
        self.rect.left,self.rect.top=mouse_pos[0]-self.rect.width/2,mouse_pos[1]-15

    def reset(self):
        self.rect.left,self.rect.top=width,height

class BackpackSpace(pygame.sprite.Sprite):
    def __init__(self,width,height):
        pygame.sprite.Sprite.__init__(self)

        self.width,self.height=width,height
        self.image=pygame.image.load('images/backpack_space.png').convert_alpha()
        self.rect=self.image.get_rect()
        self.rect.left,self.rect.top=width,height
        self.i=0
        self.j=0

    def arise(self,base_pos):
        self.rect.left,self.rect.top=base_pos[0]+self.i*98+8,base_pos[1]+self.j*98+38

    def with_move(self,base_pos):
        self.rect.left,self.rect.top=base_pos[0]+self.i*98+8,base_pos[1]+self.j*98+38

    def self_move(self,mouse_pos):
        self.rect.left,self.rect.top=mouse_pos[0]-self.rect.width/2,mouse_pos[1]-self.rect.height/2

class BackpackIcon(pygame.sprite.Sprite):
    def __init__(self,width,height):
        pygame.sprite.Sprite.__init__(self)

        self.width,self.height=width,height
        self.image=pygame.image.load('images/backpack_icon1.png').convert_alpha()
        self.image2=pygame.image.load('images/backpack_icon2.png').convert_alpha()
        self.image3=pygame.image.load('images/backpack_icon3.png').convert_alpha()
        self.rect=self.image.get_rect()
        self.rect.left,self.rect.top=width-self.rect.width,height/2-self.rect.height/2


pygame.init()
width,height=1366,768
screen=pygame.display.set_mode((width,height))
running = True
mouse_down=False
exchange_code=False
delay=60000
backpack_spacexx=pygame.sprite.Group()

backpack_base=BackpackBase(width,height)
backpack_icon=BackpackIcon(width,height)
for i in range(4):
    for j in range(4):
        a=BackpackSpace(width,height)
        a.i=i
        a.j=j
        backpack_spacexx.add(a)
for b in backpack_spacexx:
    if b.i==3 and b.j==3:
        b.image=pygame.image.load('images/brick.png')
        t=b

while running:
    if delay==0:
        delay=60000
    delay-=1
    mouse_pos=pygame.mouse.get_pos()
    mouse=pygame.mouse.get_pressed()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if not(exchange_code):
                exchange_code=True
                for b in backpack_spacexx:
                    if b.rect.left<mouse_pos[0]<b.rect.right and b.rect.top<mouse_pos[1]<b.rect.bottom:
                        t=b
                
            if backpack_icon.rect.left<mouse_pos[0]<backpack_icon.rect.right and backpack_icon.rect.top<mouse_pos[1]<backpack_icon.rect.bottom:
                mouse_down=True
            if backpack_base.rect.left<mouse_pos[0]<backpack_base.rect.right and backpack_base.rect.top<mouse_pos[1]<backpack_base.rect.bottom-4*98:
                mouse_down=True
        if event.type == pygame.MOUSEBUTTONUP:
            if backpack_base.rect.left<mouse_pos[0]<backpack_base.rect.right and backpack_base.rect.top<mouse_pos[1]<backpack_base.rect.bottom-4*98:
                mouse_down=False
            if exchange_code==True:
                exchange_code=False
                for b in backpack_spacexx:
                    if b.rect.left<mouse_pos[0]<b.rect.right and b.rect.top<mouse_pos[1]<b.rect.bottom:
                        t.i,b.i=b.i,t.i
                        t.j,b.j=b.j,t.j
                        
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                pygame.quit()
                sys.exit()
                
    screen.fill((255,255,255))
    if exchange_code:
        t.self_move(mouse_pos)
    if backpack_base.switch:
        if backpack_base.rect.left<mouse_pos[0]<backpack_base.rect.right and backpack_base.rect.top<mouse_pos[1]<backpack_base.rect.bottom-4*98:
            if mouse_down:
                backpack_base.move(mouse_pos)
        screen.blit(backpack_base.image,backpack_base.rect)
        for b in backpack_spacexx:
            if not(exchange_code):
                b.with_move(backpack_base.rect)
            screen.blit(b.image,b.rect)
    if backpack_icon.rect.left<mouse_pos[0]<backpack_icon.rect.right and backpack_icon.rect.top<mouse_pos[1]<backpack_icon.rect.bottom:
        if mouse_down:
            if backpack_base.switch:
                backpack_base.switch=False
                backpack_base.reset()
            else:
                backpack_base.arise()
                backpack_base.switch=True
                for b in backpack_spacexx:
                    b.arise(backpack_base.rect)
                mouse_down=False
        
        if (delay%2)==0:
            screen.blit(backpack_icon.image2,backpack_icon.rect)
        else:
            screen.blit(backpack_icon.image3,backpack_icon.rect)
    else:
        screen.blit(backpack_icon.image,backpack_icon.rect)
        
    pygame.display.flip()



